import { message } from 'ant-design-vue';
import request from "@/api/index";
interface dataSourceType {
  [propName: string]: any;
}
export const checkCreatSiteForm = async (_rule: dataSourceType = {}, value: any) => {
  console.log(_rule, value, '_rule, value,');
  if (_rule.required) {
    if (value === '' || value == undefined || (Array.isArray(value) && !value.length)) {// 必填项验证非空并排除非必须项
      return Promise.reject(`${_rule.msg1}`);
    }
    if (value) {// 没变化没校验的必要
      if (_rule.api && _rule.fullField) { // 没填值没有校验的必要
        var params = deleteKey(_rule.key, _rule)
        let res = await request[`${_rule.api}`](params);
        if (!res) {
          return Promise.reject(`${_rule.msg2}`);
        } else {
          return Promise.resolve();
        }
      } else {
        return Promise.resolve()
      }

    }
  } else {
    return Promise.resolve()
  }


};

const deleteKey = (key: string, obj: dataSourceType = {}) => {
  let params: dataSourceType = {};
  params[key] = obj.fullField;
  if (key == 'siteDomain' || key == 'siteDomainAlias') {
    params['siteProtocol'] = obj.params['siteProtocol']
  }
  return params
}